/**
 * Used by AutoFilter as a tooltip to set the filter value for a column.
 * Pass the values and coords and this will fire onValueSelected with 
 * a new filter value
 * 
 * @author maulin
 */
dojo.provide("medryx.widgets._GridPlugins._AutoFilterTooltip");
dojo.require("dijit._Widget");
dojo.require("dijit._Templated");
dojo.require("dijit.Dialog");


//TODO: upgrade select to ComboBox
dojo.declare("medryx.widgets._GridPlugins._AutoFilterTooltip", [dijit._Widget, dijit._Templated], {
	templatePath: dojo.moduleUrl("medryx","widgets/_GridPlugins/_AutoFilterTooltip.html"),
	widgetsInTemplate:true,
	column:"Column",
	
	onValueChanged:function(value) {
    },
	
	_onChange:function(e) {
		if (this.values.selectedIndex === -1) {
			value = null;
		} else {
			value = this.values.options[this.values.selectedIndex].value;
		}
		this.onValueChanged(value);
		this.currentValue = value;
		this.close();
	},
	
	/**
	 * set the values of the selector
	 * @param {Object} values, array of strings
	 */
	updateValues:function(values) {
		var sel = this.values;
		sel.options.length = 0;//reset the options array
        sel.options[0] = new Option("Any", "*");
		
        var currentValue = typeof(this.currentValue) !== 'undefined'  ? this.currentValue : "*";
		var selectedIndex = 0;
		
		dojo.forEach(values, function(value) {
			if (value === currentValue) {
                selectedIndex = sel.options.length;
            }
			if (value)
            sel.options[sel.options.length] = new Option(value, value);
		})
      
	    if (currentValue == "") {
            selectedIndex = sel.options.length;
        }
		if (currentValue === "*") {
            selectedIndex = 0;
        }
		sel.options[sel.options.length] = new Option("[Blank]", "");
		sel.selectedIndex = selectedIndex;
	},
	
	/**
	 * show this dialog at the given coordinates, values
	 * @param {Object} x
	 * @param {Object} y
	 */
	show:function(x, y, values) {
		this.updateValues(values);
		dijit.popup.open({popup:this.tooltip, x:x, y:y});
	}, 
	close:function() {
		dijit.popup.close(this.tooltip);
	}
})

